package com.hxs.learn.schedule.config;

import com.hxs.learn.schedule.factory.AutowiredSpringBeanJobFactory;
import lombok.Data;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * Quartz 核心配置
 *
 * @author HuXinsheng
 */
@Configuration
@ConfigurationProperties(prefix = "system")
@Data
public class QuartzConfiguration {

    private String env;

    /**
     * 指定quartz.properties
     *
     * @return Properties
     * @throws IOException
     */
    @Bean
    public Properties quartzProperties() throws IOException {
        System.out.println(env);
        String properties = "/quartz.properties";
        if (!StringUtils.isEmpty(env)) {
            properties = "/quartz-" + env + ".properties";
        }
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource(properties));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /**
     * 设置spring applicationContext为JobFactory
     *
     * @return SchedulerFactoryBean
     */
    @Bean(name = "schedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setAutoStartup(true);
        //延时5秒启动
        factory.setStartupDelay(5);
        factory.setQuartzProperties(quartzProperties());
        return factory;
    }

    /**
     * 设置spring applicationContext为JobFactory
     *
     * @param applicationContext
     * @return
     */
    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext) {
        AutowiredSpringBeanJobFactory jobFactory = new AutowiredSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }

    /**
     * @param jobFactory job工厂
     * @return Scheduler
     * @throws IOException
     * @throws SchedulerException
     */
    @Bean(name = "scheduler")
    public Scheduler scheduler(JobFactory jobFactory) throws IOException, SchedulerException {
        Scheduler scheduler = schedulerFactoryBean().getScheduler();
        scheduler.setJobFactory(jobFactory);
        return scheduler;
    }

    /**
     * quartz初始化监听器
     */
    @Bean
    public QuartzInitializerListener executorListener() {
        return new QuartzInitializerListener();
    }
}
